import logging

from django.core.cache import cache
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response

from home.models import NavCategory, Kecheng, Banner
from home.ser import NavCategorySerializer, KechengSerializer, BannersSerializer

logger = logging.getLogger('django')


# Create your views here.

# class HomeView(APIView):
#     def get(self, request):
#         logger.info('Hello World')
#         logger.error('Hello World')
#         logger.debug('Hello World')
#         return Response({'message': 'Hello World'})

class NavCateGoryAPIView(APIView):
    def get(self, request):
        """
        获取导航分类接口
        :param request:
        :return:
        """
        try:
            queryset = NavCategory.objects.filter(is_show=True, is_deleted=False)
            ser = NavCategorySerializer(queryset, many=True)
            logger.info(f"成功获取导航分类列表,共{len(ser.data)}条记录")
            return Response({
                "code": 1,
                "message": "SUCCESS",
                "data": ser.data
            })

        except Exception as e:
            logger.error(f"获取导航分类列表失败,失败原因:{e}")
            return Response({
                "code": 0,
                "message": str(e),
                "data": []
            })


class KechengAPIView(APIView):
    def get(self, request):
        try:
            queryset = Kecheng.objects.filter(is_deleted=False, is_show=True, level=1).order_by('level')
            ser = KechengSerializer(queryset, many=True)
            logger.info(f"成功获取课程列表, 共{len(ser.data)}条记录")
            return Response({
                "code": 1,
                "msg": "SUCCESS",
                "data": ser.data
            })
        except Exception as e:
            logger.error(f"获取课程列表失败, 失败原因:{e}")
            return Response({
                "code": 0,
                "msg": f"获取课程列表失败, 失败原因:{e}",
                "data": []
            })


class BannerAPIView(APIView):

    def get(self, request):
        try:
            # 从redis缓存中获取轮播图列表
            banner_key = "majing:home:banner_list"
            banner_redis = cache.get(banner_key)
            if banner_redis:
                logger.info(f"从缓存中获取轮播图列表, 共{len(banner_redis)}条记录")
                return Response({
                    "code": 1,
                    "msg": "SUCCESS FORM REDIS",
                    "data": banner_redis
                })
            queryset = Banner.objects.filter(is_deleted=False, is_show=True)
            ser = BannersSerializer(queryset, many=True)

            cache.set(banner_key, ser.data, timeout=60 * 60 * 24)
            logger.info(f"成功从mysql数据库中获取轮播图列表, 共{len(ser.data)}条记录")
            return Response({
                "code": 1,
                "msg": "SUCCESS FROM MYSQL",
                "data": ser.data
            })
        except Exception as e:
            logger.error(f"获取轮播图列表失败, 失败原因:{e}")
            return Response({
                "code": 0,
                "msg": f"获取轮播图列表失败, 失败原因:{e}",
                "data": []
            })
